import { addUrlPerFix } from "@/basa/utils";

export default {
  data() {
    return {
      printOption: {}
    };
  },
  computed: {
    finePageUrl() {
      return `${addUrlPerFix(
        process.env.VUE_APP_FINEREPORT_SERVLET_URL
      )}?viewlet=${this.encodedFilePath}&orgId=${
        this.$store.getters.userDetal.orgId
      }&t=${new Date().getTime()}`;
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      var eventMethod = window.addEventListener
        ? "addEventListener"
        : "attachEvent";
      var eventer = window[eventMethod];
      var messageEvent =
        eventMethod === "attachEvent" ? "onmessage" : "message";

      const that = this;
      eventer(messageEvent, function(e) {
        // if (e.origin !== 'http://the-trusted-iframe-origin.com') return;

        if (
          e.origin.startsWith(
            addUrlPerFix(process.env.VUE_APP_FINEREPORT_DOMAIN)
          )
        ) {
          var obj = e.data;
          if (obj.eventName === "printOption") {
            that.printOption = obj.data;
          }
        }
      });
    },
    handlePrint() {
      this.finePrint();
      // this.ccuiPrint();
    },
    finePrint() {
      if (Object.keys(this.printOption).length) {
        const reportletsObj = [
          { reportlet: this.filePath, ...this.printOption }
        ];
        const printurl = addUrlPerFix(
          process.env.VUE_APP_FINEREPORT_SERVLET_URL
        );
        const reportlets = JSON.stringify(reportletsObj).replace(/"\//g, "'");
        const config = {
          printUrl: printurl,
          isPopUp: true,
          data: {
            reportlets: reportlets // 需要打印的模板列表
          },
          printType: 0, // 打印类型，0为零客户端打印，1为本地打印
          ieQuietPrint: false // IE静默打印设置 true为静默，false为不静默
        };
        this.postEvent(config, "doURLPrint");
      } else {
        this.$message({
          message: "请先点击查询按钮",
          type: "warning",
          duration: 3000,
          showClose: true,
          ccuiCustom: true
        });
      }
    },
    handlePrintAll() {
      const printurl = addUrlPerFix(process.env.VUE_APP_FINEREPORT_SERVLET_URL);
      const reportlets = `[{reportlet: "${this.filePath}"]`;
      const config = {
        printUrl: printurl,
        isPopUp: true,
        data: {
          reportlets: reportlets // 需要打印的模板列表
        },
        printType: 1, // 打印类型，0为零客户端打印，1为本地打印
        pageType: 0, // 打印类型，0为零客户端打印，1为本地打印
        ieQuietPrint: false // IE静默打印设置 true为静默，false为不静默
      };
      this.postEvent(config, "doURLPrint");
    },
    ccuiPrint() {
      const tableHtml = document.getElementById("reportFrame").contentWindow
        .contentPane.$contentPane[0];
      const el = document.getElementById("print-page");
      el.innerHTML = tableHtml.innerHTML;
      window.print();
    },
    fineExport(reportName, params, type) {
      if (Object.keys(this.printOption).length) {
        this.postEvent(
          { reportName: reportName, params: params, type: type },
          "CCUIFineExport"
        );
      } else {
        this.$message({
          message: "请先点击查询按钮",
          type: "warning",
          duration: 3000,
          showClose: true,
          ccuiCustom: true
        });
      }
    },
    fineExportAll(type) {
      this.postEvent({ type }, "CCUIFineExportAll");
    },
    postEvent(data, eventName) {
      const frame = document.getElementById("reportFrame");
      frame.contentWindow.postMessage(
        { data: data, eventName: eventName },
        addUrlPerFix(process.env.VUE_APP_FINEREPORT_DOMAIN)
      );
    }
  },
  beforeDestroy() {
    window.removeEventListener("message");
  }
};
